package com.hzsk.lisence.util;

import com.hzsk.lisence.entity.response.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description :
 * @Date : 2024/8/27
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Slf4j
public class HttpUtils {

    private HttpUtils() {
    }

    private static CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    /**
     * @param url
     * @param paramsJson
     * @param headsMap
     * @return
     * @throws IOException
     */
    public static HttpResult httpPost(String url, String paramsJson, Map<String, String> headsMap) throws IOException {
        HttpResult httpResult = new HttpResult();
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(3000)
                    .setSocketTimeout(3000).setConnectTimeout(3000).build();
            httpPost.setConfig(config);
            if (headsMap != null && !headsMap.isEmpty()) {
                headsMap.forEach((key, value) -> {
                    httpPost.addHeader(key, value);
                });
            }else{
                headsMap=new HashMap<>();
            }
            if (headsMap.containsKey("Content-type")) {
                httpPost.addHeader("Content-type", "application/json;charset=utf-8");
            }
            StringEntity stringEntity = new StringEntity(paramsJson, "UTF-8");
            stringEntity.setContentType("application/json");
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity result = response.getEntity();
                String resultStr = null;
                if (result != null) {
                    resultStr = EntityUtils.toString(result, "UTF-8");
                }
                httpClient.close();
                response.close();
                httpResult.setStatus(HttpStatus.SC_OK);
                httpResult.setResult(resultStr);
                return httpResult;
            } else {
                httpResult.setStatus(response.getStatusLine().getStatusCode());
                httpResult.setResult("");
                return httpResult;
            }
        }catch (Exception ex) {
            httpResult.setStatus(HttpStatus.SC_NOT_FOUND);
            httpResult.setResult(ex.getMessage());
            return httpResult;
        }
    }

    /**
     * @param url
     * @return
     * @throws Exception
     */
    public static String httpGet(String url,Map<String,String> headerMap) {
        try {
            HttpGet httpGet = new HttpGet(url);
            for(String key:headerMap.keySet()) {
                httpGet.addHeader(key,headerMap.get(key));
            }
            RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(3000)
                    .setSocketTimeout(3000).setConnectTimeout(3000).build();
            httpGet.setConfig(config);
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if(response!=null) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }else {
                return null;
            }
        }catch (Exception e) {
            return null;
        }
    }
    
}
